#此代码无法递归创建目录，请老师同学们帮我分析一下，十分谢谢
from pathlib import Path
import random
import shutil

homepath = Path('D:/test')
path1 = Path('a/b/c/d/')
dstpath = homepath / path1

if homepath.exists():
    shutil.rmtree(homepath)
dstpath.mkdir(parents=True,exist_ok=True)

for i in range(50):
    x = random.choice(path1.parents)
    l1 = [random.randrange(97,122) for j in range(4)]
    nstr = ''
    for a in l1:
        nstr = nstr + chr(a)
    filename = nstr + '.txt'
    Path(homepath/x/filename).touch(exist_ok=True)

srcpath = Path(homepath / 'a')
dstpath = Path(homepath / 'dest')

shutil.rmtree(dstpath,ignore_errors=True)
def ignore(src,names):
    set1 = set()
    for name in names:
        turple1 = tuple(chr(i) for i in range(97, 120)) # 这里turple放外面，是不是更好一些
        if name.startswith(turple1):
            set1.add(name)
    return set1

shutil.copytree('D:/test/a','D:/test/dest',ignore=ignore)
